#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/bk/apis/job/get_task.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
from django import forms
from common.forms import BaseComponentForm
from components.component import Component
from common.constants import API_TYPE_Q
from .toolkit import tools, configs

class GetTask(Component):
    """
    apiLabel 查询作业模板
    apiMethod GET
    
    ### 功能描述
    
    查询作业模板
    
    ### 请求参数
    
    {{ common_args_desc }}
    
    #### 接口参数
    
    | 字段                 |  类型      | 必选   |  描述      |
    |----------------------|------------|--------|------------|
    | app_id               |  int       | 是     | 业务ID |
    | name                 |  string    | 否     | 作业名称 |
    | creater              |  string    | 否     | 作业创建人 |
    | last_modify_user     |  string    | 否     | 最后修改人 |
    | create_time_start    |  string    | 否     | 创建起始时间，YYYY-MM-DD格式 |
    | create_time_end      |  string    | 否     | 创建结束时间，YYYY-MM-DD格式 |
    | last_modify_time_start |  string  | 否     | 最后修改起始时间，YYYY-MM-DD格式 |
    | last_modify_time_end |  string    | 否     | 最后修改结束时间，YYYY-MM-DD格式 |
    
    ### 请求参数示例
    
    ```python
    {
        "app_code": "esb_test",
        "app_secret": "xxx",
        "bk_token": "xxx",
        "app_id": 46,
        "name": "hotest",
        "creater": "12345",
        "last_modify_user": "12345",
        "create_time_start": "2016-02-22 23:12:34",
        "create_time_end": "2016-02-22 23:12:34",
        "last_modify_time_start": "2016-02-22 23:12:34",
        "last_modify_time_end": "2016-02-22 23:12:34"
    }
    ```
    
    ### 返回结果示例
    
    ```python
    {
        "result": true,
        "code": "00",
        "message": "",
        "data": [
            {
                "account": "",
                "name": "hotest",
                "creater": "12345",
                "stepNum": 1,
                "serverSetId": 0,
                "nmStepBeanList": [],
                "lastModifyTime": "2016-02-22 23:12:34",
                "appId": 46,
                "id": 190,
                "ipList": "",
                "createTime": "2016-02-22 23:12:34",
                "lastModifyUser": "12345"
            },
        ],
    }
    ```
    """
    sys_name = configs.SYSTEM_NAME
    api_type = API_TYPE_Q
    host = configs.host

    class Form(BaseComponentForm):
        app_id = forms.IntegerField(label=u'业务ID', required=True)
        name = forms.CharField(label=u'作业名称', required=False)
        creater = forms.CharField(label=u'创建人', required=False)
        last_modify_user = forms.CharField(label=u'最后修改人', required=False)
        create_time_start = forms.DateField(label=u'创建起始时间', required=False, input_formats=['%Y-%m-%d'])
        create_time_end = forms.DateField(label=u'创建结束时间', required=False, input_formats=['%Y-%m-%d'])
        last_modify_time_start = forms.DateField(label=u'最后修改起始时间', required=False, input_formats=['%Y-%m-%d'])
        last_modify_time_end = forms.DateField(label=u'最后修改结束时间', required=False, input_formats=['%Y-%m-%d'])

        def clean(self):
            data = self.cleaned_data
            return {'appId': data['app_id'],
               'name': data['name'],
               'creater': data['creater'],
               'lastModifyUser': data['last_modify_user'],
               'createTimeStart': data['create_time_start'].strftime('%Y-%m-%d') if data['create_time_start'] else '',
               'createTimeEnd': data['create_time_end'].strftime('%Y-%m-%d') if data['create_time_end'] else '',
               'lastModifyTimeStart': data['last_modify_time_start'].strftime('%Y-%m-%d') if data['last_modify_time_start'] else '',
               'lastModifyTimeEnd': data['last_modify_time_end'].strftime('%Y-%m-%d') if data['last_modify_time_end'] else ''
               }

    def handle(self):
        data = self.form_data
        data['operator'] = self.current_user.username
        client = tools.JOBClient(self.outgoing.http_client)
        data = tools.get_basic_json(action='queryTask', params=data)
        result = client.post(self.host, data=data)
        self.response.payload = result
